package simple;

import JaCoP.constraints.XmulYeqZ;
import JaCoP.constraints.knapsack.Knapsack;
import JaCoP.constraints.knapsack.KnapsackItem;
import JaCoP.core.IntVar;
import JaCoP.core.Store;
import JaCoP.search.DepthFirstSearch;
import JaCoP.search.IndomainMax;
import JaCoP.search.PrintOutListener;
import JaCoP.search.Search;
import JaCoP.search.SelectChoicePoint;
import JaCoP.search.SimpleSelect;
import JaCoP.search.SmallestDomain;

public class Shopping {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		long T1, T2, T;
		T1 = System.currentTimeMillis();
		shop();
		T2 = System.currentTimeMillis();
		T = T2 - T1;
		System.out.println("\n\t*** Execution time = " + T + " ms");
	}

	private static void shop() {
		Store store = new Store();
		IntVar cost = new IntVar(store, "cost", 0, 500);
		IntVar capacity = new IntVar(store, "capacity", 0, 500);
		
		KnapsackItem[] items = new KnapsackItem[4];
		int[] itemWeights = {50,300,400,250};
		int[] itemProfits = {50,450,520,100};
		IntVar[] intVars = new IntVar[4];
		for (int i = 0; i < 4; i++) {
			intVars[i] = new IntVar(store, "item" + i, 0, 100);
			items[i] = new KnapsackItem(intVars[i],
					itemWeights[i], itemProfits[i]);
		}
		IntVar neg = new IntVar(store, "neg",-1,-1);
		IntVar negCost = new IntVar(store,"negcost", -500,0);
		store.impose(new XmulYeqZ(cost,neg,negCost));
		
		Knapsack knapsack = new Knapsack(items, capacity, cost);
		
		store.impose(knapsack);
		
	
		Search<IntVar> label = new DepthFirstSearch<IntVar>();

		SelectChoicePoint<IntVar> select = new SimpleSelect<IntVar>(intVars, new SmallestDomain<IntVar>(),
				new IndomainMax<IntVar>());

		label.setSolutionListener(new PrintOutListener<IntVar>());
		
		boolean Result = label.labeling(store, select, negCost);

		if (Result) {
			System.out.println("Sunglasses, Swimsuit, Towel, Cooler");
			label.printAllSolutions();
		} else {
			System.out.println("\n*** No");
		}

	}

}
